<?php
/**
 *
 * User/IndexAction.class.php (设计师模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class GuestbookaddAction extends BaseAction
{

    function _initialize()
    {
        parent::_initialize();
        $_GET =get_safe_replace($_GET);
        $this->url='index.php?g=User&m=Guestbook&a=index';


        $this->dao = M('User');
        $this->assign('bcid',0);
        $user = $this->dao->find($this->_userid);
        $this->user=$user;
        $this->assign('vo',$user);
        unset($_POST['status']);
        unset($_POST['groupid']);
        unset($_POST['amount']);
        unset($_POST['point']);


    }

    public function index()
    {


        $this->display();
    }



    //预约
    public function add()
    {
        $this->assign('title','提交需求');
        $designid = $_GET['designid'];
        if ($designid) {
            $this->assign ('designid',$designid);
        }
        $this->display();
    }

    public function insert()
    {
        $sjsid=$_REQUEST["sjsid"];

        $mobileYzm = get_safe_replace($_POST['mobileyzm']);

//print_r($list);exit;

        $module='Guestbook';
        $db=M($module);
        $_POST['ip'] = get_client_ip();
        if(empty($_POST['title'])){
            $this->error("姓名不能为空!");
        }
        if(empty($_POST['telephone'])){
            $this->error("手机不能为空!");
        }

        if(empty($_POST['bcity'])){
            $this->error("城市不能为空!");
        }

        if(empty($mobileYzm)){
            $this->error("验证码不能为空!");

        }

          if( md5($mobileYzm) != $_SESSION['tel_verify']){

          $this->error(L('验证码错误'));
          }



         $time_cha=time()-$_SESSION['time_verify'];
         if($time_cha > '120'){
            $this->error(L('验证码已过期'));
         }




        if(!$db->create($_POST)) {
            $this->error($db->getError());
        }

        $budget= $_POST['budget'];
        if($budget) {
            $budget_r = M('Label')->where("genre=19 and title='" . $budget . "'")->find();
            $db->budget_order = $budget_r['listorder'];
        }
        if($sjsid){
            $u = M('User');
            $list=$u->where("id=".$sjsid)->find();
            $db->sjsname=$list['username'];
            $db->sjsid=$list['id'];
            $db->typeid=1;
            $u->where("id=".$sjsid)->setInc('yuyue_num');
        }
        $db->createtime = time();
        $db->lang = LANG_ID;
        $db->keywords=$_POST['title'];

        $result	=$db->add();


        if($result) {
            //添加用户动态
            $userdate['title']='发布了【设计需求】';
            $userdate['url']='index.php?m=Guestbook&a=show&id='.$result;
            $userdate['userid']=$this->_userid;
            $userdate['conid']=$result;
			$userdate['module']='Guestbook';
            $userdate['createtime']=time();
            $userdate['lang']='1';
            $userdate['status']='1';
            $ud=M("Dynamic")->add($userdate);

            //$message="有用户提交了设计需求<br> 称呼：".$_POST['title']."<br>电话：".$_POST['telephone']."<br>".jgarea($_POST['bprovince']).jgarea($_POST['bcity'])."<br>类型：".$_POST['itemType']."<br>面积：".$_POST['itemArea']."<br>预算：".$_POST['budget'];
            //$r = sendmail('1002074283@qq.com',  '提交需求提示-' . $this->Config['site_name'], $message, $this->Config);

            $this->success('提交成功,等待审核！',U('Mobile/Index/index'));

        }else{
            $this->error('提交失败，请稍后再试！');
        }
    }






    public function yuyue(){
        $designid=$_GET['userid'];
        $u = M('User');
        $list=$u->where("id=".$designid)->find();
        $data['designid']=$list['id'];
        $data['userid']=$this->_userid;
        $data['createtime']=time();
        $db=M('Yuyue');
        $result	=$db->add($data);
        if($result) {
            $u->where("id=".$designid)->setInc('yuyue_num');
            $this->success('预约成功！');
        }else{
            $this->error('预约失败，请稍后再试！');
        }

    }

    public function delete()
    {
        $id = $_GET['id'];
        $userid = $this->_userid;
        $dao = M('Case');
        $list = $dao->field('pics,thumb')->where("id=" . $id)->find();
        $pics = explode(":", $list['pics']);

        $result = $dao->where("id=" . $id)->delete();
        if ($result) {
            @unlink('./' . $list['thumb']);
            foreach ($pics as $k => $v) {
                $r = @unlink($v);

            }

            $this->success('操作成功', $this->url);
        }


    }





}

